/**
 * 
 */
package ken.app.xmlhandler;

import java.util.ArrayList;
import java.util.List;

import ken.app.bean.GetStoreInfoBean;
import ken.app.bean.NewsListBean;
import ken.app.bean.QueryPersonalBabyBean;
import ken.app.bean.SportBabyListBean;
import ken.app.bean.TreeAllBean;


import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.util.Log;

/**
 * @author ken.chung
 * 
 */
public class GetStoreInfoXMLHandler extends DefaultHandler {
	
	/* 變數宣告 */
	private boolean in_item = false;
	private boolean in_id = false;
	private boolean in_name = false;
	private boolean in_lng = false;
	private boolean in_lat = false;
	private boolean in_distance = false;
	private boolean in_url = false;
	private boolean in_type = false;
	
	private boolean in_result = false;
	
	private List<GetStoreInfoBean> list;
	private GetStoreInfoBean mGetStoreInfoBean;
	private String title = "";
	private StringBuffer buf = new StringBuffer();

	/* 將轉換成List<News>的XML資料回傳 */
	public List<GetStoreInfoBean> getParsedData() {
		return list;
	}

	/* XML文件開始解析時呼叫此method */
	@Override
	public void startDocument() throws SAXException {
		if (list == null) {
			list = new ArrayList<GetStoreInfoBean>();
		}
	}

	/* XML文件結束解析時呼叫此method */
	@Override
	public void endDocument() throws SAXException {
	}

	/* 解析到Element的開頭時呼叫此method */
	@Override
	public void startElement(String namespaceURI, String localName,
			String qName, Attributes atts) throws SAXException {
		
			if (localName.equals("item")) {
				this.in_item = true;
				/* 解析到item的開頭時new一個News物件 */
				mGetStoreInfoBean = new GetStoreInfoBean();
				Log.d("ken", buf.toString());
			} else if (localName.equals("id")) {
				if (this.in_item) {
					this.in_id = true;
				} 
			} else if (localName.equals("name")) {
				if (this.in_item) {
					this.in_name = true;
				}
			} else if (localName.equals("lng")) {
				if (this.in_item) {
					this.in_url = true;
				}
			} else if (localName.equals("lat")) {
				if (this.in_item) {
					this.in_lat = true;
				}
			}  else if (localName.equals("type")) {
				if (this.in_item) {
					this.in_type = true;
				}
			} else if (localName.equals("distance")) {
				if (this.in_item) {
					this.in_distance = true;
				}
			} else if (localName.equals("url")) {
				if (this.in_item) {
					this.in_url = true;
				}
			}
		
		buf.setLength(0);
	}

	/* 解析到Element的結尾時呼叫此method */
	@Override
	public void endElement(String namespaceURI, String localName, String qName)
			throws SAXException {
		if (localName.equals("item")) {
			this.in_item = false;
			/* 解析到item的結尾時將News物件寫入List中 */
			if (mGetStoreInfoBean.getDistance()!=-1) {
				list.add(mGetStoreInfoBean);
			}
		} else if (localName.equals("id")) {
			if (this.in_item) {
				/* 設定News物件的title */
				mGetStoreInfoBean.setStoreId(buf.toString().trim());
				buf.setLength(0);
				this.in_id = false;
			} 
		} else if (localName.equals("name")) {
			if (this.in_item) {
				/* 設定News物件的link */
				mGetStoreInfoBean.setName(buf.toString().trim());
				buf.setLength(0);
				this.in_name = false;
			}
		} else if (localName.equals("lng")) {
			if (this.in_item) {
				/* 設定News物件的link */
				mGetStoreInfoBean.setLng(Double.valueOf(buf.toString().trim()));
				buf.setLength(0);
				this.in_lng = false;
			}
		} else if (localName.equals("lat")) {
			if (this.in_item) {
				/* 設定News物件的link */
				mGetStoreInfoBean.setLat(Double.valueOf(buf.toString().trim()));
				buf.setLength(0);
				this.in_lat = false;
			}
		} else if (localName.equals("distance")) {
			if (this.in_item) {
				/* 設定News物件的link */
				mGetStoreInfoBean.setDistance(Integer.valueOf(buf.toString().trim()));
				buf.setLength(0);
				this.in_distance = false;
			}
		} else if (localName.equals("type")) {
			if (this.in_item) {
				/* 設定News物件的link */
				mGetStoreInfoBean.setType(Integer.valueOf(buf.toString().trim()));
				buf.setLength(0);
				this.in_type = false;
			}
		}  else if (localName.equals("url")) {
			if (this.in_item) {
				/* 設定News物件的link */
				mGetStoreInfoBean.setUrl(buf.toString().trim());
				buf.setLength(0);
				this.in_url = false;
			}
		}
	}

	/* 取得Element的開頭結尾中間夾的字串 */
	@Override
	public void characters(char ch[], int start, int length) {
		if (this.in_item ) {
			buf.append(ch, start, length);
		}
	}
}